package com.yanyi.music.controller;

import com.alibaba.fastjson.JSONObject;
import com.yanyi.music.domain.ListSong;
import com.yanyi.music.service.impl.ListSongServiceImpl;
import com.yanyi.music.utils.Consts;
import io.swagger.annotations.Api;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.Map;

/**
 * 歌单的歌曲管理
 */
@Api(tags = "歌单API")
@RestController
@RequestMapping("/listSong")
public class ListSongController {
    @Autowired
    private ListSongServiceImpl listSongService;

    /**
     * 给歌单添加歌曲
     */
    @RequestMapping(value = "/add", method = RequestMethod.POST)
    public Object addListSong(HttpServletRequest request) {
        JSONObject jsonObject = new JSONObject();
        //歌曲id
        String songId = request.getParameter("songId").trim();
        //歌单id
        String songListId = request.getParameter("songListId").trim();
        ListSong listSong = new ListSong();
        listSong.setSongId(Integer.parseInt(songId));
        listSong.setSongListId(Integer.parseInt(songListId));
        int flag = listSongService.insert(listSong);
        if (flag == 1) {
            jsonObject.put(Consts.CODE, 1);
            jsonObject.put(Consts.MSG, "保存成功");
            return jsonObject;
        }
        jsonObject.put(Consts.CODE, 0);
        jsonObject.put(Consts.MSG, "保存失败");
        return jsonObject;
    }

    /**
     * 根据歌单id查询歌曲
     */
    @RequestMapping(value = "/detail", method = RequestMethod.GET)
    public Object detail(HttpServletRequest request) {
        Map<String, Object> map = new HashMap<>();
        map.put("songListId", request.getParameter("songListId"));
        return listSongService.finadAll(map);
    }

    /**
     * 删除歌单里的歌曲
     */
    @RequestMapping(value = "/delete", method = RequestMethod.DELETE)
    public Object del(HttpServletRequest request) {
        return listSongService.deleteBySongIdAndSongListId(Integer.valueOf(request.getParameter("songId")),
                Integer.valueOf(request.getParameter(
                        "songListId")));
    }
}
